/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.manager.model;

import java.util.List;
import java.util.Map;
import org.hutech09.jsale.bsn.ISupplierManager;
import org.hutech09.jsale.entity.Supplier;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author Nam
 */
public class LazySupplierDataModel extends LazyDataModel<Supplier> {

    private ISupplierManager supplierManager;

    public LazySupplierDataModel(ISupplierManager supplierManager) {
        this.supplierManager = supplierManager;
    }

    @Override
    public Supplier getRowData(String rowKey) {
        long id = Long.parseLong(rowKey);
        return supplierManager.findByID(id);
    }

    @Override
    public Object getRowKey(Supplier object) {
        return object.getId();
    }

    @Override
    public List<Supplier> load(int first, int pagesize, String sortField, SortOrder sortOrder, Map<String, String> filter) {
        System.err.println("managedbean: " + filter);
        List<Supplier> result = supplierManager.findPaging(first, pagesize, sortField, sortOrder == SortOrder.DESCENDING, filter);
        int count = (int) supplierManager.count(filter);
        this.setRowCount(count);
        return result;
    }

    public void setSupplierManager(ISupplierManager supplierManager) {
        this.supplierManager = supplierManager;
    }
}
